﻿#include "stdafx.h"
#include "AstIf.h"
#include "AstType.h"
#include "utility.h"
#include "CodeGenerate/BlockGen.h"
#include "CodeGenerate/IfGen.h"

using namespace llvm;

void AstIf::draw(std::ostream & os) {
	dotLable(os, "if ", "diamond");

	std::string n = nodeId;
	os << n << " -> " << condition->nodeId << "[label=\"condition\"];" << std::endl;
	condition->draw(os);
}

CodeGen * AstIf::makeGen(SiContext * parent){
	auto g = condition->makeGen(parent);
	auto*type = g->type;
	
	BlockGen* thenGen = nullptr;
	BlockGen* elseGen = nullptr;

	if (type==AstType::staticTrue) {
		return makeCodeGenList(parent, thenBlock);
	}
	else if (type == AstType::staticFalse) {
		if (elseBlock.empty()) return nullptr;
		return makeCodeGenList(parent, elseBlock);
	}

	auto *p = new IfGen();
	p->condition = g;
	p->thenBlock = makeCodeGenList(parent, thenBlock);
	if (!elseBlock.empty()) {
		p->elseBlock = makeCodeGenList(parent, elseBlock);
	}
	return p;
}
